﻿using Microsoft.EntityFrameworkCore;
using PHMEE.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace PHMEE
{
    public class VesselRepository : BaseRepository<Vessel, Guid>, IVesselRepository
    {
        public VesselRepository(IDbContextProvider<PHMEEDbContext> dbContextProvider)
        : base(dbContextProvider)
        {
        }

        public async Task<List<T>> GetAllByTypeAsync<T>() where T : Vessel
        {
            var dbset = await GetDbSetAsync();
            var list = dbset.OfType<T>().ToList();
            return list;
        }

        public override async Task<IQueryable<Vessel>> WithDetailsAsync()
        {
            var query = await GetQueryableAsync();
            return query.Include(item => item.Units)
                        .ThenInclude(item => item.Equipments)
                        .ThenInclude(item => item.Components);
        }

        public async Task<FlowMeterModeEnum?> GetTemplateModeByVesselIdAsync(Guid vesselId)
        {
            var dbSet = await GetDbSetAsync();
            return await dbSet
                .Where(vessel => vessel.Id == vesselId)
                .Select(vessel => vessel.FlowMeterMode)
                .FirstOrDefaultAsync();
        }
    }
}
